<html>
<head><meta charset="utf-8"><title>Using rustc_private code with Cargo · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html">Using rustc_private code with Cargo</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231911903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231911903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Will Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231911903">(Mar 26 2021 at 04:01)</a>:</h4>
<p>I'm working on a <a href="https://github.com/willcrichton/rust-slicer">static analysis tool</a> that uses rustc_private. My goal is to invoke this tool from VSCode when inside of a Cargo-structured Rust project. The main challenge is knowing what arguments to pass to rustc_driver, e.g. values of <code>--extern</code> for dependencies. Right now, I'm doing something <a href="https://github.com/willcrichton/rust-slicer/blob/master/src/bin/rust-slicer-cli.rs#L13">incredibly hacky</a> by scraping the output of <code>cargo check -v</code>.</p>
<p>Is there a better way to collect build information from Cargo for use with rustc_private?</p>



<a name="231913060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231913060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231913060">(Mar 26 2021 at 04:18)</a>:</h4>
<p>I'm unclear what you're trying to do - do you want your tool instead of <code>rustc</code>? You can do that with <code>RUSTC=rustc-slicer cargo check</code></p>



<a name="231967498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231967498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Will Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231967498">(Mar 26 2021 at 13:51)</a>:</h4>
<p>My use case is analogous to rust-analyzer. The goal is that while you have a Rust file open, you can invoke the analyzer to extract information about the file that's visualized by the IDE. But I don't want to rebuild the entire compiler like rust-analyzer does <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> Because I'm running my analyzer through rustc_private, I need to invoke rustc_driver with the appropriate <code>--extern</code> and other flags for the current project to type-check correctly.</p>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> this is the same issue I had with example-analyzer, and I actually copied <a href="https://github.com/willcrichton/example-analyzer/blob/master/src/main.rs#L132-L191">roughly the same code</a>. </p>
<p>I hadn't thought about using the RUSTC environment variable, that could work I suppose. Although I don't know exactly the requirements for that, e.g. it seems like it assumes the binary has certain capabilities like responding to <code>-vV</code>.</p>



<a name="231967868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231967868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231967868">(Mar 26 2021 at 13:53)</a>:</h4>
<p>The RUSTC binary basically has to pass all arguments to rustc_driver.</p>



<a name="231969855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231969855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231969855">(Mar 26 2021 at 14:05)</a>:</h4>
<p>Honestly you might be able to hack it by running your binary instead of rustdoc <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span> cargo doesn't use too many of the fancy flags for rustdoc I don't think</p>



<a name="231982160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231982160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231982160">(Mar 26 2021 at 15:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo/near/231969855">said</a>:</p>
<blockquote>
<p>Honestly you might be able to hack it by running your binary instead of rustdoc <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span> cargo doesn't use too many of the fancy flags for rustdoc I don't think</p>
</blockquote>
<p>oh gawd what a hack <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="231983394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/231983394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Will Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#231983394">(Mar 26 2021 at 15:24)</a>:</h4>
<p>I think I'll try to use the new <code>--unit-graph</code> feature instead. Only challenge there: is there a reliable way to get the path to an <code>rmeta</code> file? Eg I know that <code>a</code> depends on <code>b</code>, so after I run <code>cargo check</code>, should I just grep for <code>target/debug/deps/libb-*.rmeta</code>?</p>



<a name="232011477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Using%20rustc_private%20code%20with%20Cargo/near/232011477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Will Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Using.20rustc_private.20code.20with.20Cargo.html#232011477">(Mar 26 2021 at 18:25)</a>:</h4>
<p>Update: the <code>unit-graph</code> approach works pretty well! I'm able to run my analysis tool on an arbitrary file in a Cargo workspace once <code>cargo check</code> has completed. For anyone interested: </p>
<p><a href="https://github.com/willcrichton/rust-slicer/blob/405e9261d3996680514bd4f95dfe7a4f1212406a/src/bin/rust-slicer-cli/unit_graph.rs">https://github.com/willcrichton/rust-slicer/blob/405e9261d3996680514bd4f95dfe7a4f1212406a/src/bin/rust-slicer-cli/unit_graph.rs</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>